package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.model.TbTypeTemplate;
import com.dongdongshop.model.TbTypeTemplateExample;
import com.dongdongshop.service.TemplateService;
import com.dongdongshop.vo.BrandVo;
import com.dongdongshop.vo.TemplateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TemplateServiceImpl implements TemplateService {

    @Resource
    private TbTypeTemplateMapper tbTypeTemplateMapper;


    @Override
    public List<TemplateVo> templateList(TemplateVo templateVo) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(templateVo.getName())){
            criteria.andNameLike("%"+templateVo.getName()+"%");
        }
        List<TbTypeTemplate> template = tbTypeTemplateMapper.selectByExample(example);
        return template.stream().map(templat ->{
            TemplateVo vo = new TemplateVo();
            BeanUtils.copyProperties(templat,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void deleteTemplate(Long id) {
            tbTypeTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(TemplateVo vo) {
        TbTypeTemplate template = new TbTypeTemplate();
        BeanUtils.copyProperties(vo, template);
        tbTypeTemplateMapper.insertSelective(template);
    }

    @Override
    public TemplateVo toupdate(Long id) {
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        TemplateVo vo = new TemplateVo();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        return vo;
    }

    @Override
    public TbTypeTemplate showbyid(Long id) {
        return tbTypeTemplateMapper.selectByPrimaryKey(id);    }
    @Override
    public void updateTemplate(TemplateVo templateVo) {
        TbTypeTemplate tbType= new TbTypeTemplate();
        BeanUtils.copyProperties(templateVo,tbType);
        tbTypeTemplateMapper.updateByPrimaryKeySelective(tbType);
    }

    @Override
    public void deleteallTbTypeTemplate(List<Long> ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        List<TbTypeTemplate>  template= tbTypeTemplateMapper.deleteByExample(example);
         template.stream().map(templat ->{
            TemplateVo vo = new TemplateVo();
            BeanUtils.copyProperties(templat,vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
